package com.lagou.tp.util;

import java.util.*;

/**
 * @AUTHOR: yb.feng
 * @DATE: 2020/6/15 23:37
 * @DESC:
 */
public class TpMapUtil {

    private static Map<String, List<Long>> tpMap = new HashMap<>();

    public static void addCost(String methodName, Long cost){
        if (tpMap.get(methodName) == null){
            List<Long> list = new ArrayList<>();
            list.add(cost);
            tpMap.put(methodName, list);
        }else {
            List<Long> list = tpMap.get(methodName);
            list.add(cost);
        }
    }

    static {
        new Timer().scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                for (Map.Entry<String, List<Long>> entry : tpMap.entrySet()) {
                    List<Long> list = entry.getValue();
                    list.sort(Long::compareTo);
                    int index90 = (int) (list.size() * 0.9);
                    System.out.println(entry.getKey() + " tp90:" + list.get(index90));
                    int index99 = (int) (list.size() * 0.99);
                    System.out.println(entry.getKey() + " tp99:" + list.get(index99));
                    System.out.println("========================");
                }
            }
        }, 0, 5000);
    }
}
